package com.pioneer.gui.invoice.model;

import java.awt.Graphics;

import javax.swing.table.TableModel;
import javax.swing.JTable;
import javax.swing.event.TableModelListener;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import javax.swing.event.TableModelEvent;
import javax.swing.event.EventListenerList;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import java.util.Vector;
import java.util.HashMap;

import com.pioneer.gui.invoice.model.InvoiceModel;
import com.pioneer.gui.invoice.util.Constants;
import com.pioneer.gui.invoice.util.DropDownDataFactory;
import com.pioneer.gui.invoice.util.InvoiceDAO;
import com.pioneer.beans.invoice.InvoiceDetailModel;

public class InvoiceTableModel
implements TableModel,Constants
{
	private final int TOTAL_COL=6;
	private Vector _tableData=new Vector();
	private String [] _colNames;
	private InvoiceDAO invoiceDAO;	
	private EventListenerList _tableModelListenerList;	
	private HashMap itemTypeMap=new HashMap();
	private HashMap qtyTypeMap=new HashMap();
	private String invoiceId;
	
	public InvoiceTableModel()
	{
		this.invoiceDAO=new InvoiceDAO();
	    _tableModelListenerList=new EventListenerList();
	    _colNames=new String[TOTAL_COL];
	    _colNames[0]="Item Num";
	    _colNames[1]="Type";
	    _colNames[2]="Description";
	    _colNames[3]="Qty";
	    _colNames[4]="UOM";
	    _colNames[5]="UnitPrice";
	    this.loadReferenceList();
	}
	
	private void loadReferenceList()
	{	
		DropDownDataFactory dropDownDataFactory=new DropDownDataFactory();
		Vector itemTypes=dropDownDataFactory.getVector(ITEM_TYPE);
		for (int count=0; count<itemTypes.size();count++)
		{
			Object itemtypeDesc=((Object[])itemTypes.get(count))[1];
			Object itemtypeKey=((Object[])itemTypes.get(count))[0];
			itemTypeMap.put(itemtypeDesc,itemtypeKey);
		}
		Vector qtyTypes=dropDownDataFactory.getVector(QTY_TYPE);
		for (int count=0; count<qtyTypes.size();count++)
		{
			Object qtytypeDesc=((Object[])qtyTypes.get(count))[1];
			Object qtytypeKey=((Object[])qtyTypes.get(count))[0];
			qtyTypeMap.put(qtytypeDesc,qtytypeKey);
		}
	}
	
	public void setTableData(Vector tableData,String invoiceId)
	{
		this.invoiceId=invoiceId;
        buildTableVector(tableData);
	}
	
	class CellContents
	{
		Object _value;
		boolean _isEditable=true;
	}	
	
	public int getRowCount()
	{
		return _tableData.size();
	}
	
	public int getColumnCount()
	{
		return this._colNames.length;
	}

	public String getColumnName(int columnIndex)
	{
		return this._colNames[columnIndex];
	}
	
	public Class getColumnClass(int columnIndex)
	{
		if (this._tableData.size()>0)
		{
			CellContents[] rowData=(CellContents[])this._tableData.get(0);
			Object value=rowData[columnIndex]._value;
			Class myClass=null;
			if (value!=null)
			{
				myClass= value.getClass();
			}
			return myClass;
		}
		return null;
	}
	
	public boolean isCellEditable(int rowIndex, int columnIndex)
	{
		if (this._tableData.size()>0)
		{
			return ((CellContents)  ( (CellContents[])this._tableData.get(rowIndex)  )[columnIndex]   )._isEditable;
		}
		return false;
	}

	public Object getValueAt(int rowIndex, int columnIndex)
	{
		if (this._tableData.size()>0)
		{
			return ((CellContents)  ( (CellContents[])this._tableData.get(rowIndex)  )[columnIndex]   )._value;
		}
		return null;
	}
                         
	public void setValueAt(Object aValue,
                       int rowIndex,
                       int columnIndex)
	{
		CellContents[] rowData=(CellContents[])this._tableData.get(rowIndex);
		if (columnIndex==0)
		{
			String desc=invoiceDAO.findDescByItemNo(aValue.toString());			
			rowData[2]._value=desc;
		}
		if (columnIndex==3) aValue=new Integer(aValue.toString());
		if (columnIndex==5) aValue=new Float(aValue.toString());
		//System.out.println("The class is "+aValue.getClass().getName());
		rowData[columnIndex]._value=aValue;		
	}
	
	public void deleteRow(int rowNum)
	{
		System.out.println("The row to be deleted is "+rowNum);
		this._tableData.remove(rowNum);
		this.fireChange();
	}
	
	public void addRow()
	{
		CellContents[] _rowData=new CellContents[TOTAL_COL];
						
		CellContents cellContents=new CellContents();			
		cellContents._value=new String();
		_rowData[0]=cellContents;
			
		cellContents=new CellContents();
		cellContents._value=new String(STANDARD);
		_rowData[1]=cellContents;
			
		cellContents=new CellContents();
		cellContents._value=new String();
		cellContents._isEditable=false;
		_rowData[2]=cellContents;
			
		cellContents=new CellContents();
		cellContents._value=new Integer(0);
		_rowData[3]=cellContents;
			
		cellContents=new CellContents();
		cellContents._value=new String(PIECES);
		_rowData[4]=cellContents;
			
		cellContents=new CellContents();
		cellContents._value=new Float(0);
		_rowData[5]=cellContents;
		_tableData.add(_rowData);		
		this.fireChange();
	}

                       
	public void addTableModelListener(TableModelListener l)
	{
		this._tableModelListenerList.add (TableModelListener.class, l);
	}
	
	public void removeTableModelListener(TableModelListener l)
	{
		this._tableModelListenerList.remove(TableModelListener.class, l);
	}		
	
	private void buildTableVector(Vector allDetails)
	{
		_tableData=new Vector();
		for (int i=0; i<allDetails.size(); i++)
		{
			InvoiceDetailModel invoiceDetailModel=(InvoiceDetailModel)allDetails.get(i);			
			
			CellContents[] _rowData=new CellContents[TOTAL_COL];
						
			CellContents cellContents=new CellContents();			
			cellContents._value=invoiceDetailModel.getItemNo();
			_rowData[0]=cellContents;
			
			cellContents=new CellContents();
			cellContents._value=invoiceDetailModel.getItemType();
			_rowData[1]=cellContents;
			
			cellContents=new CellContents();
			cellContents._value=invoiceDetailModel.getItemDesc();
			_rowData[2]=cellContents;
			
			cellContents=new CellContents();
			cellContents._value=new Integer(invoiceDetailModel.getItemQty());
			_rowData[3]=cellContents;
			
			cellContents=new CellContents();
			cellContents._value=invoiceDetailModel.getItemQtyType();
			_rowData[4]=cellContents;
			
			cellContents=new CellContents();
			cellContents._value=new Float(invoiceDetailModel.getPrice());
			_rowData[5]=cellContents;
			
			_tableData.add(_rowData);
		}
		this.fireChange();		
	}
	
	public void doSave()
	{  	
    	
    	Vector tableData=new Vector();
		for (int rowCount=0;rowCount<this.getRowCount();rowCount++)
		{
			String [] rowData=new String[6];
			for (int colCount=0;colCount<this.getColumnCount();colCount++)
			{
				//System.out.println("Value is "+this.getValueAt(rowCount, colCount));
				if (colCount==1)
					rowData[colCount]=itemTypeMap.get(this.getValueAt(rowCount, colCount).toString()).toString();
				else if (colCount==4)
					rowData[colCount]=qtyTypeMap.get(this.getValueAt(rowCount, colCount).toString()).toString();
				else
					rowData[colCount]=this.getValueAt(rowCount, colCount).toString();
			}
			tableData.add(rowData);
		}
		invoiceDAO.saveDetails(this.invoiceId,tableData);
	}
		
	
	protected void fireChange()
	{
		TableModelEvent c = new TableModelEvent(this);
		Object[] listeners = _tableModelListenerList.getListenerList();
		for (int i = listeners.length-2; i >= 0; i -= 2)
		{
			if (listeners[i] == TableModelListener.class)
			{
				TableModelListener cl = (TableModelListener)listeners[i+1];
				cl.tableChanged(c);
				
			}
		}
	}
	
                              
}
	